package com.zhhq.controller;

import com.zhhq.enums.ResultEnum;
import com.zhhq.model.Result;
import com.zhhq.model.User;
import com.zhhq.model.VerificationCode;
import com.zhhq.service.IUserService;
import com.zhhq.utils.EmailUtil;
import com.zhhq.utils.ResultUtil;
import com.zhhq.utils.VerificationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created by HeChaobin on 2017-07-13.
 */
@RestController
@RequestMapping(value = "/users")
@Api(value = "/users", description = "用户增删改、登陆等操作")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping(value = "/{loginname}")
    @ApiOperation(
            value = "根据登陆名获取用户信息",
            notes = "只有管理员才有该权限",
            httpMethod="GET"
           )
    public Result getUser(@PathVariable String loginname) {

        User user=iUserService.getUserByLoginName(loginname);
        if (user!=null) {
            return ResultUtil.success(user);
        }
        else {
            return ResultUtil.error(ResultEnum.USERNOTEXISTS);
        }
    }

    @GetMapping(value = "/name/{loginname}")
    @ApiOperation(
            value = "检测用户是否存在",
            notes = "检测用户是否存在",
            httpMethod="GET"
    )
    public Result checkUserName(@PathVariable String loginname) {

        User user=iUserService.getUserByLoginName(loginname);
        if (user!=null) {
            return ResultUtil.success(loginname);
        }
        else {
            return ResultUtil.error(ResultEnum.USERNOTEXISTS);
        }
    }

    @GetMapping(value = "/verification/{loginname}")
    @ApiOperation(
            value = "获取重置密码的验证码",
            notes = "自动发送验证码到该用户的注册邮箱",
            httpMethod="GET"
    )
    public Result sendVerificationCode(@PathVariable String loginname) throws Exception {
       User user= iUserService.getUserByLoginName(loginname);
       if (user==null)
       {
           return ResultUtil.error(ResultEnum.USERNOTEXISTS);
       }
        Result result;
        result= VerificationUtil.getVerificationCode(loginname);
        if (result.getCode()!=0){
            return  result; // 获取验证码失败
        }

        Integer code= (Integer) result.getData();
        String html = "重置智慧后勤管理系统登录密码的验证码为：<font color=\"blue\" size=\"8\">"+code+"</font>  此验证5分钟内有效";
        EmailUtil.sendEmail(user.getEmail(), "验证码", html, true);

        return ResultUtil.success(user.getEmail());
    }

    @GetMapping(value = "/verification/{loginname}/{code}")
    @ApiOperation(
            value = "验证验证码是否正确",
            notes = "",
            httpMethod="GET"
    )
    public Result verifyCode(@PathVariable String loginname,@PathVariable Integer code) throws Exception {
        Result result;
        result= VerificationUtil.validateCode(loginname,code);

        return result;
    }

    @PostMapping(value = "/login")
    @ApiOperation(
            value = "用户登陆",
            notes = "用户输入登录名和密码进行登陆系统",
            httpMethod="POST"
    )
    public Result login(@RequestParam("loginname") String loginName, @RequestParam("loginpass") String loginPass) throws Exception {


        return ResultUtil.success(iUserService.login(loginName,loginPass));
    }

    @PostMapping(value = "/password")
    @ApiOperation(
            value = "重置密码",
            notes = "重置指定用户名的登陆密码",
            httpMethod="POST"
    )
    public  Result modifyPassword(@RequestParam("loginname") String loginName, @RequestParam("loginpass") String loginpass)
    {
        iUserService.updatePassword(loginName,loginpass);

        return ResultUtil.success();
    }


    /**
     * 新增
     *
     * @param user 控制器信息
     * @return
     */
    @PostMapping(value = "", consumes = "application/json")
    @ApiOperation(
            value = "新增用户",
            notes = "新增用户",
            httpMethod="POST"
    )
    public Result addUser(@RequestBody User user) throws Exception {

        return ResultUtil.success(iUserService.addUser(user));
    }

    @DeleteMapping(value = "/{loginname}")
    @ApiOperation(
            value = "删除用户",
            notes = "删除指定登录名的用户"
    )
    public  Result deleteUserByLoginName(@PathVariable String loginname)
    {
        iUserService.deleteUser(loginname);
        return  ResultUtil.success();
    }

    @PutMapping(value = "", consumes = "application/json")
    @ApiOperation(
            value = "修改用户",
            notes = "修改用户"
    )
    public Result updateUser(@RequestBody User user) throws Exception {

        iUserService.updateUser(user);
        return ResultUtil.success();
    }

}
